//
//  HistorySearchRecord.m
//  CarLife
//
//  Created by yu on 2017/11/13.
//  Copyright © 2017年 jpas. All rights reserved.
//

#import "HistorySearchRecord.h"

#define HISTORY_RECORD @"HISTORY_RECORD"

@interface HistorySearchRecord ()

@property (nonatomic, strong) NSUserDefaults *userDefaults;

@end

@implementation HistorySearchRecord

// 保存
- (void)saveSearchRecord:(NSString *)record
{
    NSArray *history = [self.userDefaults arrayForKey:HISTORY_RECORD];
    NSMutableArray *historyList = [NSMutableArray arrayWithArray:history];
    
    if (![historyList containsObject:record]) {
        [historyList insertObject:record atIndex:0];
    }
    [self.userDefaults setObject:historyList forKey:HISTORY_RECORD];
}

// 读取
- (NSMutableArray *)getHistoryRecord
{
    NSArray *history = [self.userDefaults objectForKey:HISTORY_RECORD];
    NSMutableArray *historyList = [NSMutableArray arrayWithArray:history];
    return historyList;
}

// 删除
- (void)deleteHistoryRecord:(NSUInteger)index
{
    NSArray *history = [self.userDefaults arrayForKey:HISTORY_RECORD];
    NSMutableArray *historyList = [NSMutableArray arrayWithArray:history];
    [historyList removeObjectAtIndex:index];
    [self.userDefaults setObject:historyList forKey:HISTORY_RECORD];
}

// 删除全部
- (void)deleteAllHistoryRecord
{
    [self.userDefaults removeObjectForKey:HISTORY_RECORD];
}

- (NSUserDefaults *)userDefaults {
    if (!_userDefaults) {
        _userDefaults = [NSUserDefaults standardUserDefaults];
    }
    return _userDefaults;
}

@end
